// pages/welfare/welfare.js
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    vipUrl: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  getData() {
    let that = this
    db.collection("welfare")
      .orderBy('time', 'desc')
      .limit(3)
      .get()                   //获取根据查询条件筛选后的集合数据  
      .then(res => {
        let getList = res.data.map(function (item) {
          let month = item.time.getMonth() + 1
          if (month < 10) {
            month = '0' + month
          }
          let day = item.time.getDate()
          if (day < 10) {
            day = '0' + day
          }
          item.time = month + '-' + day
          return item
        })
        that.setData({
          list: getList
        })
      })
      .catch(err => {
        wx.showToast({ title: '加载失败', mask: true, icon: 'error' })
        console.error('初始化init()：', err)
      })
    db.collection("imgurl")
      .where({
        type: 'vip'
      })
      .get()
      .then(res => {
        let temp = []
        res.data.forEach(element => {
          temp.push(element.url)
        });
        this.setData({
          vipUrl: temp
        })
      })
      .catch(err => {
        console.error('getUrls()：', err)
      })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '阿里云盘最新福利码(扩容码)',
      path: '/pages/welfare/welfare',//这里是被分享的人点击进来之后的页面
    }
  },

  onCopy(e) {
    const { data } = e.currentTarget.dataset
    wx.setClipboardData({
      data,
      success: () => {
        wx.showToast({ title: '复制成功' })
      }
    })
  },

  // 跳转扩容活动
  onArticle() {
    wx.navigateTo({
      url: '/pages/article/article'
    })
  }
})